/*
 * ksDefines.h
 *
 *  Created on: Aug 3, 2011
 *      Author: wang
 */

#ifndef KSDEFINES_H_
#define KSDEFINES_H_

#include <cstdio>
#include <string>
#include "ksLog.h"

/* block 类型 */
#define kBlockFile      1
#define kBlockMain      2
#define kBlockPart      3

#define kBlockInfo      4
#define kBlockCover     5
#define kBlockCatalog   6
#define kBlockChapter   7
#define kBlockPay       8
#define kBlockResource  9
#define kBlockContent   10

const char* blockType2String( unsigned short id );

const unsigned short  kBlockFileTag = kBlockFile;
const unsigned short  kBlockMainTag = kBlockMain;
const unsigned short  kBlockPartTag = kBlockPart;

/* 数据长度定义 */
#define kBlockTypeMaxLength 2
#define kBlockIDMaxLength   2
#define kCompressFlagLength 1
#define kEncryptFlagLength  1
#define kPlaceHoldLength    2
#define kSubBlockCountFeildLength kPlaceHoldLength
#define kDataLengthFeildLength 4
#define kInfoLengthFeildLength 4
#define kNextPartOffsetFeildLength 4
#define kBlockEntryLengthFeildLength 4
#define kBlockEntryOffsetFeildLength 4

#define kAtomBlockHeaderLength \
              (kBlockTypeMaxLength \
            + kBlockIDMaxLength \
            + kCompressFlagLength \
            + kEncryptFlagLength \
            + kPlaceHoldLength \
            + kDataLengthFeildLength)

#define kContainerBlockHeaderLength\
              (kBlockTypeMaxLength \
            + kBlockIDMaxLength \
            + kCompressFlagLength \
            + kEncryptFlagLength \
            + kSubBlockCountFeildLength \
            + kDataLengthFeildLength\
            + kInfoLengthFeildLength\
            + kNextPartOffsetFeildLength)

#define kBlockEntryLength\
            (kBlockTypeMaxLength \
            + kBlockIDMaxLength \
            + kBlockEntryLengthFeildLength \
            + kBlockEntryOffsetFeildLength)

#define Log(p)    std::cout<<p<<endl
#define LogObj(p)  ksLog::log( __FILE__, __LINE__, p )

/* 四字节对齐 */
#define _align( n, a )  ((n + a - 1) & (~(a - 1)))
#define _align4( n )  ((n + 3) & (~3))
/* 根据容量拷贝字符串 */
void safe_strcopy(char* dest, const char* sour, unsigned int capacity);

#endif /* KSDEFINES_H_ */
